<?php


namespace app\controller\admin;


use app\common\exception\ApiException;
use app\model\coach\UserCoachModel;
use app\model\config\RecycleModel;
use app\model\partner\UserPartnerModel;
use app\model\school\SchoolModel;
use think\facade\Db;

class RecycleController extends BaseAdminController
{

    function setModel()
    {
        return new RecycleModel();
    }

    function setWithModel()
    {
        // TODO: Implement setWithModel() method.
    }

    function setSearch()
    {
        return [
            '='=>['type'],
            'like' => ['title'],
        ];
    }

    function setSort()
    {
        // TODO: Implement setSort() method.
    }

    public function back()
    {
        Db::startTrans();
        try {
            $id = $this->request->param('id', 0, 'int');
            $info = $this->setModel()->find($id);
            if (!$info) {
                throw new ApiException('数据不存在');
            }
            $type = $info['type'];
            if ($type == 1) {
                $schoolInfo = SchoolModel::withTrashed()->find($info['uid']);
                $schoolInfo->restore();
            }
            if ($type == 2) {
                $userInfo = UserCoachModel::withTrashed()->find($info['uid']);
                $userInfo->restore();
            }
            if ($type == 3) {
                $userInfo = UserPartnerModel::withTrashed()->find($info['uid']);
                $userInfo->restore();
            }
            $info->delete();
            Db::commit();
            return $this->success();
        } catch (\Exception $exception) {
            Db::rollback();
            return $this->error($exception);
        }
    }

    public function clear()
    {
        Db::startTrans();
        try {
            Db::execute('truncate table mp_recycle');
            Db::commit();
            return $this->success();
        } catch (\Exception $exception) {
            Db::rollback();
            return $this->error($exception);
        }
    }
}